package easy._101_SymmetricTree;

public class SymmetricTree {

    public boolean valueCheck(TreeNode p,TreeNode q){
        if(p==null && q==null){
            return true;
        }
        if(p==null || q==null){
            return false;
        }
        if(p.val==q.val){
            return valueCheck(p.left,q.right) && valueCheck(p.right,q.left);
        }else{
            return false;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if(root==null) return true;
        return valueCheck(root.left,root.right);
    }

    //更快的解法
    public boolean isSymmetric1(TreeNode root) {
        return root == null || isSymmetric(root.left, root.right);
    }

    private boolean isSymmetric(TreeNode left, TreeNode right) {
        int nu = (left == null ? 1 : 0) + (right == null ? 1 : 0);
        if (nu == 1)
            return false;
        if (nu == 2)
            return true;
        return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
    }
}
